#include "../../inc/skySocket.h"
#include "../../inc/skyString.h"
#include "../../inc/skyBuffer.h"

#ifdef _SYMBIAN

namespace Sky
{
		Bool Socket::open(String* ipAddr,u32 port){
			_sockets = CSIPExSocketEngine::NewLC(*this);
			TInetAddr addr;
			addr.Input(TPtrC(ipAddr->toCharString()));
			addr.SetPort(port);
			_sockets->ConnectL(addr,(TUint32)-1);
            _reading = vFalse;
            _writing = vFalse;
            return vTrue;
		}
		Void Socket::close(){
			_sockets->Stop();
			delete _sockets;
		}
		Bool Socket::read(Socket::DoneCallback cb){
            if(_reading)
            {
                xError0A("current is reading");
                return vFalse;
            }
		    _sockets->Read();
			_rcb = cb;
			return vTrue;
		}
		Bool Socket::write(Buffer* buffer,Socket::DoneCallback cb){
            if(_writing)
            {
                xError0A("current is writting");
                return vFalse;
            }
            //BufferPtr pBuffer;
            //if(!buffer->isOnHeap())
            //{
            //    pBuffer = skynew (Buffer(buffer));
            //    pBuffer->cloneFrom(buffer);
            //    pBuffer->incRef();
            //}
            _sockets->Write(TPtrC8(buffer->getBuffer(),buffer->getLength()));
            _wcb = cb;
			return vTrue;
		}
}


#endif
